/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.servlet;

import com.kamus.manager.KosakataBaruMgr;
import com.kamus.model.KosakataBaru;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Vector;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import sax.MyElement;
import sax.MySAXParserBean;

/**
 *
 * @author omega
 */
public class NewWordServlet extends HttpServlet {

    private static final String SEPARATOR_WORD = "~";

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String author = "", gaul = "", ind = "";
        PrintWriter out = response.getWriter();
        ServletInputStream sis = request.getInputStream();
        MySAXParserBean p = new MySAXParserBean();
        try {
            Vector v = p.parse(sis);
            MyElement element;
            for (int i = 0; i < v.size(); i++) {
                element = (MyElement) v.elementAt(i);
                String tag = element.getQname();
                if (tag.equals("author")) {
                    author = element.getValue();
                } else if (tag.equals("gaul")) {
                    gaul = element.getValue();
                } else if (tag.equals("ind")) {
                    ind = element.getValue();
                }
            }
        } catch (Exception e) {
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            return;
        }
        try {
            Date now = new Date();
            String[] arrGaul = gaul.split(SEPARATOR_WORD);
            String[] arrInd = ind.split(SEPARATOR_WORD);
            KosakataBaruMgr kbm = KosakataBaruMgr.getInstance();
            KosakataBaru kb = new KosakataBaru();
            for (int i = 0; i < arrInd.length; i++) {
                kb.setAuthor(author);
                kb.setKataGaul(arrGaul[i]);
                kb.setKataInd(arrInd[i]);
                kb.setTanggal(now);
                kbm.insertKata(kb);
            }
            //response to client
            response.setStatus(response.SC_OK);
            out.print(generateResponse(1, "Success Add New Word To Server. Waiting for updating database by admin"));
        } catch (Exception e) {
            response.setStatus(response.SC_OK);
            out.print(generateResponse(1, e.getMessage()));
        }
    }

    private String generateResponse(int code, String msg) {
        StringBuffer dataResponse = new StringBuffer();
        dataResponse.append("<xml>").append("<code>").append(code).append("</code>").append("<msg>").append(msg).append("</msg>").append("</xml>");
        return dataResponse.toString();
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
